package com.example.zjhuojback.controller;

import com.example.zjhuojback.common.Result;
import com.example.zjhuojback.entity.*;
import com.example.zjhuojback.service.ContestService;
import com.example.zjhuojback.service.ForumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/contest")
public class ContestController {
    @Autowired
    private ContestService contestService;

    // 分页获取
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Contest> contests = contestService.selectAll();
        return Result.success(contests);
    }

    @GetMapping("/selectById")
    public Result selectById(Integer id) {
        Contest contest = contestService.selectById(id);
        return Result.success(contest);
    }

    /**
     * 添加题目
     * @param contest
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Contest contest) {
        try {
            contestService.insertContest(contest);
        } catch (Exception e) {
            return Result.error("插入数据错误");
        }
        return Result.success();
    }

    /**
     * 更新比赛
     * @param contest
     * @return
     */
    @PutMapping("/update/{id}")
    public Result update(@PathVariable Integer id, @RequestBody Contest contest) {
        try {
            // 设置ID
            contest.setId(id);
            contestService.updateContest(contest);
        } catch (Exception e) {
            return Result.error("更新数据错误: " + e.getMessage());
        }
        return Result.success();
    }
    /**
     * 更新比赛状态
     * @param id
     * @return
     */
    @PutMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Integer id, @PathVariable Integer status) {
        try {
            contestService.updateStatus(id, status);
        } catch (Exception e) {
            return Result.error("更新数据错误: " + e.getMessage());
        }
        return Result.success();
    }
    /**
     * 获取指定比赛已加入的题目
     * @param contestId
     * @return
     */
    @GetMapping("/{contestId}/questions")
    public Result getCompetitionQuestions(@PathVariable Integer contestId) {
        List<ContestProblem> contestProblemList = contestService.getCompetitionQuestions(contestId);
        return Result.success(contestProblemList);
    }

    /**
     * 获取参赛用户
     * @param contestId
     * @return
     */
    @GetMapping("/selectAllSubmissionByContestId/{contestId}")
    public Result selectAllSubmission(@PathVariable Integer contestId) {
        List<User> users = contestService.selectAllUser(contestId);
        return Result.success(users);
    }

    /**
     * 判断用户是否AC了当前这题
     */
    @GetMapping("/selectAllSubmission/{contestId}/{problemId}/{userId}")
    public Result checkProblemStatus(@PathVariable Integer contestId,
                                     @PathVariable Integer problemId,
                                     @PathVariable Integer userId,
                                     @RequestParam("startTime") String startTime) {
        try {
            // 将传入的 startTime 字符串解析为 LocalDateTime 对象
            LocalDateTime contestStartTime = LocalDateTime.parse(startTime);
            // 调用服务方法并传入比赛开始时间
            SubmissionResult submissionResult = contestService.findSubmissionByUserAndProblem(contestId, problemId, userId, contestStartTime);
            // 返回成功的结果
            return Result.success(submissionResult);
        } catch (Exception e) {
            // 返回错误的结果
            return Result.error();
        }
    }


}
